// CalculationSheet.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CalculationSheet.h"
#include "CalculationPage.h"


// CCalculationSheet

IMPLEMENT_DYNAMIC(CCalculationSheet, CTabCtrl)

CCalculationSheet::CCalculationSheet()
{
    mwnd_currentpage = NULL;
}

CCalculationSheet::~CCalculationSheet()
{
}


BEGIN_MESSAGE_MAP(CCalculationSheet, CTabCtrl)
    ON_NOTIFY_REFLECT(TCN_SELCHANGE, &CCalculationSheet::OnTcnSelchange)
    ON_WM_SIZE()
    ON_WM_DESTROY()
END_MESSAGE_MAP()



// CCalculationSheet-Meldungshandler



void CCalculationSheet::OnTcnSelchange(NMHDR *pNMHDR, LRESULT *pResult)
{
    // select new tab
    ActivatePage(man_tab_ids[GetCurSel()]);
    *pResult = 0;
}



void CCalculationSheet::ActivatePage(UINT nr)
{
    // Delete old
    if (mwnd_currentpage)
    {
        mwnd_currentpage->ShowWindow(SW_HIDE);
        mwnd_currentpage = NULL;
    }


    // check if page is valid
    for (int t=0;t<ma_pages.GetCount();t++)
    {
        if (ma_pages[t].id == nr)
        {
            mwnd_currentpage = ma_pages[t].page;
            SetCurSel(t);
            break;
        }
    }

    if (!mwnd_currentpage)
    {
        mwnd_currentpage = ma_pages[0].page;
        SetCurSel(0);
    }


    // resize
    OnTabResize();

    mwnd_currentpage->ShowWindow(SW_SHOW);

    SETOPTION(_T("LAST_SUB2"),nr);
}



CWnd* CCalculationSheet::GetPage(UINT nr)
{
    // check if page is valid
    for (int t=0;t<ma_pages.GetCount();t++)
        if (ma_pages[t].id == nr)
            return ma_pages[t].page;

    return NULL;
}



void CCalculationSheet::OnTabResize()
{
    if (!mwnd_currentpage) return;

    // Get Sizes
    CRect rect2;
    GetItemRect(0,rect2); // just to get the right high

    CRect rect;
    GetClientRect(rect);


    // Resize sub Dialog  - 0-423. tot-486
    rect.top+=rect2.Height()+2*GetSystemMetrics(SM_CYEDGE);
    rect.left+=GetSystemMetrics(SM_CXEDGE);
    rect.bottom-=GetSystemMetrics(SM_CYEDGE);
    rect.right-=GetSystemMetrics(SM_CXEDGE);

    mwnd_currentpage->SetWindowPos(NULL,rect.left,rect.top,rect.Width(),rect.Height(),
                                   SWP_NOZORDER | SWP_NOACTIVATE);
}


void CCalculationSheet::OnSize(UINT nType, int cx, int cy)
{
    CTabCtrl::OnSize(nType, cx, cy);
    OnTabResize();
}



void CCalculationSheet::AddPage(CCalculationPage* pc, UINT id, UINT name_id)
{
    struct_page newpage;
    newpage.name.LoadString(name_id);
    newpage.id = id;
    newpage.page=pc;
    newpage.managed=false;

    InsertItem(GetItemCount(),newpage.name);
    ma_pages.Add(newpage);
    man_tab_ids.Add(id);
}


void CCalculationSheet::AddPage(CRuntimeClass* pc, UINT id, UINT name_id)
{
    struct_page newpage;
    newpage.name.LoadString(name_id);
    newpage.id = id;
    newpage.page = (CCalculationPage*)(pc->CreateObject());
    newpage.managed=true;

    VERIFY(newpage.page->Create(this));

    InsertItem(GetItemCount(),newpage.name);
    ma_pages.Add(newpage);
    man_tab_ids.Add(id);
}


void CCalculationSheet::OnDestroy()
{
    CTabCtrl::OnDestroy();

    for (int t=0;t<ma_pages.GetCount();t++)
    {
        if (ma_pages[t].managed && ma_pages[t].page)
        {
            ma_pages[t].page->DestroyWindow();
            delete(ma_pages[t].page);
            ma_pages[t].page = NULL;
        }
    }
}


void CCalculationSheet::OnRaceChanged()
{
    for (int t=0;t<ma_pages.GetCount();t++)
        if (ma_pages[t].page)
            ma_pages[t].page->OnRaceChanged();
}

void CCalculationSheet::OnLevelChanged()
{
    for (int t=0;t<ma_pages.GetCount();t++)
        if (ma_pages[t].page)
            ma_pages[t].page->OnLevelChanged();
}


void CCalculationSheet::OnClassChanged()
{
    for (int t=0;t<ma_pages.GetCount();t++)
        if (ma_pages[t].page)
            ma_pages[t].page->OnClassChanged();
}

void CCalculationSheet::OnRRChanged()
{
    for (int t=0;t<ma_pages.GetCount();t++)
        if (ma_pages[t].page)
            ma_pages[t].page->OnRRChanged();
}

void CCalculationSheet::OnPointsChanged()
{
    for (int t=0;t<ma_pages.GetCount();t++)
        if (ma_pages[t].page)
            ma_pages[t].page->OnPointsChanged();
}
